<template>
	<scroll-view scroll-y class="vip-page">
		<!-- 顶部背景 -->
		<view class="header-bg">
			<!-- 返回按钮 -->
			<view class="back-btn" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			
			<text class="crown-icon">👑</text>
			<text class="title">VIP会员特权</text>
			<text class="subtitle">开通会员，享受专属服务</text>
		</view>

		<!-- 特权列表 -->
		<view class="privileges-section">
			<view class="privilege-item" v-for="(item, index) in privileges" :key="index">
				<view class="privilege-icon">{{ item.icon }}</view>
				<view class="privilege-content">
					<text class="privilege-title">{{ item.title }}</text>
					<text class="privilege-desc">{{ item.desc }}</text>
				</view>
			</view>
		</view>

		<!-- VIP套餐 -->
		<view class="packages-section">
			<view class="section-title">选择套餐</view>
			<view class="packages-list">
				<view 
					class="package-card" 
					v-for="(pkg, index) in packages" 
					:key="index"
					:class="{ 'active': selectedPackage === index }"
					@click="selectPackage(index)"
				>
					<text class="package-name">{{ pkg.name }}</text>
					<view class="package-price">
						<text class="price-symbol">¥</text>
						<text class="price-value">{{ pkg.price }}</text>
						<text class="price-unit">/{{ pkg.duration }}</text>
					</view>
					<text class="package-original" v-if="pkg.originalPrice">原价 ¥{{ pkg.originalPrice }}</text>
					<view class="package-benefits">
						<text class="benefit-item" v-for="(benefit, idx) in pkg.benefits" :key="idx">
							{{ benefit }}
						</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 底部占位 -->
		<view class="bottom-placeholder"></view>

		<!-- 底部购买按钮 -->
		<view class="bottom-bar">
			<view class="total-info">
				<text class="total-label">合计：</text>
				<text class="total-price" v-if="selectedPackagePrice > 0">¥{{ selectedPackagePrice }}</text>
				<text class="total-price-hint" v-else>请选择套餐</text>
			</view>
			<button class="buy-btn" @click="handlePurchase">立即开通</button>
		</view>
	</scroll-view>
</template>

<script>
	import userAuth from '@/utils/userAuth.js'
	
	export default {
		data() {
			return {
				gatewayURL: 'http://localhost:8083',
				currentUserId: null,
				selectedPackage: null, // 默认不选中任何套餐
				isVip: false,
				vipExpireTime: null,
				remainingDays: 0,
				privileges: [
					{
						icon: '👑',
						title: '专属身份标识',
						desc: '尊贵VIP标识，提升个人魅力'
					},
					{
						icon: '💎',
						title: '优先推荐',
						desc: '优先展示给优质用户，提高匹配率'
					},
					{
						icon: '💌',
						title: '无限查看联系方式',
						desc: '查看心仪对象的联系方式，无限制'
					},
					{
						icon: '👀',
						title: '查看访客记录',
						desc: '查看谁看过你，主动出击'
					},
					{
						icon: '📞',
						title: '专属红娘服务',
						desc: '一对一专属红娘，贴心服务'
					},
					{
						icon: '🎉',
						title: '线下活动优先',
						desc: '优先参加平台举办的线下活动'
					}
				],
				packages: []
			}
		},
		onLoad() {
			// 获取登录用户ID - 使用修复后的工具类
			this.currentUserId = userAuth.getUserId()
			console.log('=== VIP页面调试信息 ===')
			console.log('获取到的用户ID:', this.currentUserId)
			console.log('存储的userId:', uni.getStorageSync('userId'))
			console.log('存储的userInfo:', uni.getStorageSync('userInfo'))
			console.log('存储的token:', uni.getStorageSync('token'))
			
			// 加载VIP信息
			this.loadVipInfo()
		},
		computed: {
			selectedPackagePrice() {
				if (this.selectedPackage === null || this.selectedPackage === undefined) {
					return 0
				}
				return this.packages[this.selectedPackage]?.price || 0
			}
		},
		methods: {
			// 加载VIP信息
			loadVipInfo() {
				const params = {}
				if (this.currentUserId) {
					params.userId = this.currentUserId
				}
				
				uni.request({
					url: this.gatewayURL + '/api/vip/info',
					method: 'GET',
					data: params,
					success: (res) => {
						console.log('VIP信息:', res.data)
						
						if (res.data && res.data.code === 200) {
							const data = res.data.data
							
							// 更新VIP状态
							this.isVip = data.isVip
							this.vipExpireTime = data.vipExpireTime
							this.remainingDays = data.remainingDays
							
							// 更新套餐列表
							this.packages = data.packages.map(pkg => ({
								packageId: pkg.packageId,
								name: pkg.name,
								price: pkg.price,
								originalPrice: pkg.originalPrice,
								duration: pkg.duration,
								benefits: pkg.benefits,
								recommend: pkg.recommend
							}))
							
							// 不自动选中任何套餐，让用户手动选择
						}
					},
					fail: (err) => {
						console.error('获取VIP信息失败:', err)
						uni.showToast({
							title: '加载VIP信息失败',
							icon: 'none'
						})
					}
				})
			},
			
			goBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			
			selectPackage(index) {
				this.selectedPackage = index
			},
			
			handlePurchase() {
				// 检查是否选择了套餐
				if (this.selectedPackage === null || this.selectedPackage === undefined) {
					uni.showToast({
						title: '请先选择一个套餐',
						icon: 'none'
					})
					return
				}
				
				const pkg = this.packages[this.selectedPackage]
				
				if (!pkg) {
					uni.showToast({
						title: '请选择套餐',
						icon: 'none'
					})
					return
				}
				
				// 获取用户ID
				const userId = this.currentUserId || 1
				
				uni.showModal({
					title: '确认开通',
					content: `确认开通${pkg.name}（¥${pkg.price}）？`,
					success: (res) => {
						if (res.confirm) {
							// 调用后端购买接口
							uni.showLoading({
								title: '处理中...'
							})
							
							uni.request({
								url: this.gatewayURL + '/api/vip/purchase?userId=' + userId + '&packageId=' + pkg.packageId,
								method: 'POST',
								success: (res) => {
									uni.hideLoading()
									
									if (res.data && res.data.code === 200) {
										uni.showToast({
											title: 'VIP开通成功！',
											icon: 'success',
											duration: 2000
										})
										
										// 延迟返回上一页
										setTimeout(() => {
											uni.navigateBack()
										}, 2000)
									} else {
										uni.showToast({
											title: res.data?.message || 'VIP开通失败',
											icon: 'none'
										})
									}
								},
								fail: (err) => {
									uni.hideLoading()
									console.error('VIP购买失败:', err)
									uni.showToast({
										title: '网络请求失败',
										icon: 'none'
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.vip-page {
		height: 100vh;
		background: linear-gradient(180deg, #9575CD 0%, #B39DDB 50%, #F5F5F5 100%);
	}
	
	.bottom-placeholder {
		height: 180rpx;
	}

	/* 顶部背景 */
	.header-bg {
		background: linear-gradient(135deg, #9575CD 0%, #7E57C2 100%);
		padding: 60rpx 60rpx 100rpx;
		padding-top: calc(60rpx + env(safe-area-inset-top));
		text-align: center;
		position: relative;
		overflow: hidden;
		
		&::before {
			content: '';
			position: absolute;
			top: -100rpx;
			right: -100rpx;
			width: 400rpx;
			height: 400rpx;
			background: rgba(255, 255, 255, 0.1);
			border-radius: 50%;
		}
		
		&::after {
			content: '';
			position: absolute;
			bottom: -150rpx;
			left: -150rpx;
			width: 500rpx;
			height: 500rpx;
			background: rgba(255, 255, 255, 0.1);
			border-radius: 50%;
		}
		
		.back-btn {
			position: absolute;
			left: 30rpx;
			top: calc(60rpx + env(safe-area-inset-top));
			width: 70rpx;
			height: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			z-index: 10;
			
			.back-icon {
				font-size: 48rpx;
				color: #FFFFFF;
				font-weight: bold;
			}
		}
		
		.crown-icon {
			font-size: 100rpx;
			margin-bottom: 25rpx;
			position: relative;
			z-index: 1;
			display: block;
		}
		
		.title {
			display: block;
			font-size: 44rpx;
			font-weight: bold;
			color: #FFFFFF;
			margin-bottom: 15rpx;
			position: relative;
			z-index: 1;
		}
		
		.subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(255, 255, 255, 0.9);
			position: relative;
			z-index: 1;
		}
	}

	/* 特权列表 */
	.privileges-section {
		margin: -70rpx 30rpx 30rpx;
		background-color: #FFFFFF;
		border-radius: 30rpx;
		padding: 35rpx 30rpx;
		box-shadow: 0 8rpx 30rpx rgba(149, 117, 205, 0.15);
		
		.section-title {
			font-size: 30rpx;
			font-weight: bold;
			color: #333333;
			margin-bottom: 25rpx;
			text-align: center;
		}
		
		.privilege-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 25rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.privilege-icon {
				width: 70rpx;
				height: 70rpx;
				background: linear-gradient(135deg, #E8E5FF 0%, #D5CFFF 100%);
				border-radius: 18rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
				margin-right: 20rpx;
				flex-shrink: 0;
			}
			
			.privilege-content {
				flex: 1;
				
				.privilege-title {
					display: block;
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 8rpx;
				}
				
				.privilege-desc {
					display: block;
					font-size: 22rpx;
					color: #999999;
					line-height: 1.4;
				}
			}
		}
	}

	/* 套餐列表 */
	.packages-section {
		margin: 30rpx;
		
		.section-title {
			font-size: 30rpx;
			font-weight: bold;
			color: #333333;
			margin-bottom: 25rpx;
			text-align: center;
		}
		
		.packages-list {
			display: flex;
			flex-direction: column;
			gap: 18rpx;
			
			.package-card {
				background-color: #FFFFFF;
				border-radius: 20rpx;
				padding: 30rpx;
				position: relative;
				border: 3rpx solid #EFEFEF;
				box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
				transition: all 0.3s;
				
				.package-name {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 15rpx;
					display: block;
				}
				
				.package-price {
					display: flex;
					align-items: baseline;
					margin-bottom: 8rpx;
					
					.price-symbol {
						font-size: 28rpx;
						color: #9575CD;
						font-weight: bold;
					}
					
					.price-value {
						font-size: 48rpx;
						font-weight: bold;
						color: #9575CD;
						margin: 0 5rpx;
					}
					
					.price-unit {
						font-size: 24rpx;
						color: #999999;
					}
				}
				
				.package-original {
					font-size: 22rpx;
					color: #CCCCCC;
					text-decoration: line-through;
					margin-bottom: 15rpx;
					display: block;
				}
				
				.package-benefits {
					display: flex;
					flex-wrap: wrap;
					gap: 12rpx;
					
					.benefit-item {
						background: linear-gradient(135deg, #E8E5FF 0%, #D5CFFF 100%);
						padding: 6rpx 18rpx;
						border-radius: 30rpx;
						font-size: 20rpx;
						color: #7E57C2;
					}
				}
				
				&.active {
					background-color: #F8F5FF;
					border-color: #EFEFEF;
				}
			}
		}
	}

	/* 底部购买按钮 */
	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #FFFFFF;
		padding: 20rpx 30rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.08);
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 999;
		
		.total-info {
			display: flex;
			align-items: baseline;
			
			.total-label {
				font-size: 26rpx;
				color: #666666;
			}
			
			.total-price {
				font-size: 44rpx;
				font-weight: bold;
				color: #FF6B8A;
				margin-left: 10rpx;
			}
			
			.total-price-hint {
				font-size: 28rpx;
				color: #999999;
				margin-left: 10rpx;
			}
		}
		
		.buy-btn {
			background: linear-gradient(135deg, #9575CD 0%, #7E57C2 100%);
			color: #FFFFFF;
			padding: 18rpx 50rpx;
			border-radius: 50rpx;
			font-size: 28rpx;
			font-weight: bold;
			border: none;
			box-shadow: 0 8rpx 20rpx rgba(149, 117, 205, 0.4);
			
			&::after {
				border: none;
			}
		}
	}
</style>

